package com.java110.mall.cart.storeOrderEngine;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.product.ProductGroupDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.intf.goods.IGroupBuyProductSpecInnerServiceSMO;
import com.java110.intf.goods.IProductGroupV1InnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName StoreOrderProduct
 * @Description TODO
 * @Author wuxw
 * @Date 2022/5/21 0:49
 * @Version 1.0
 * add by wuxw 2022/5/21
 **/
@Component
public class StoreOrderProduct {

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    @Autowired
    private IGroupBuyProductSpecInnerServiceSMO groupBuyProductSpecInnerServiceSMOImpl;

    @Autowired
    private IProductGroupV1InnerServiceSMO productGroupV1InnerServiceSMOImpl;

    public ProductSpecValueDto getProductSpecValueDto(JSONObject goods) {
        ProductSpecValueDto productSpecValueDto;
        int flag;//查询 产品 及价格 并且 减库存
        productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setProductId(goods.getString("productId"));
        productSpecValueDto.setValueId(goods.getString("valueId"));
        productSpecValueDto.setStoreId(goods.getString("storeId"));
        List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);
        Assert.listOnlyOne(productSpecValueDtos, "不存在 该产品信息");

        productSpecValueDto = productSpecValueDtos.get(0);
        goods.put("areaCode", productSpecValueDto.getAreaCode());
        int stock = Integer.parseInt(productSpecValueDto.getStock());

        if (stock < 1) {
            throw new IllegalArgumentException("库存不足");
        }
        ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
        productSpecValuePo.setValueId(productSpecValueDto.getValueId());
        productSpecValuePo.setProductId(productSpecValueDto.getProductId());
        productSpecValuePo.setStoreId(productSpecValueDto.getStoreId());
        productSpecValuePo.setSales((Integer.parseInt(productSpecValueDto.getSales()) + 1) + "");
        productSpecValuePo.setStock((stock - 1) + "");
        flag = productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
        if (flag < 1) {
            throw new IllegalArgumentException("减库存失败");
        }
        return productSpecValueDto;
    }


    public ProductGroupDto getGroupBuyProduct(ProductSpecValueDto productSpecValueDto) {
        ProductGroupDto productGroupDto;
        int flag;
        productGroupDto = new ProductGroupDto();
        productGroupDto.setStoreId(productSpecValueDto.getStoreId());
        productGroupDto.setProductId(productSpecValueDto.getProductId());
        productGroupDto.setValueId(productSpecValueDto.getValueId());
        productGroupDto.setShopId(productSpecValueDto.getShopId());
        List<ProductGroupDto> productGroupDtos = productGroupV1InnerServiceSMOImpl.queryProductGroups(productGroupDto);

        if (ListUtil.isNull(productGroupDtos)) {
            return null;
        }

        productGroupDto = productGroupDtos.get(0);

        return productGroupDto;
    }

}
